package com.haiwei.poi.word.sign;

import com.haiwei.poi.common.HaiweiArrayUtil;
import com.haiwei.poi.common.HaiweiStringUtil;
import com.haiwei.poi.word.exception.HaiweiSignException;
import com.haiwei.poi.word.manager.HaiweiPatternUtil;

/**
 * 标签解析.
 *
 * @date: 2022-06-25
 * @version: 1.0
 * @author: karry@haiwei.com
 */
public class HaiweiSignParser {
    
    /**
     * 判断是否为标签
     * @param sign
     * @return
     */
    public static boolean isSign(String text){
        return HaiweiPatternUtil.containPlaceHolder(text);
    }
    
    /**
     * 解析标签
     * @param sign
     * @return 标签对象
     */
    public static HaiweiTextSign parse(String sign){
        /**
         *  ##{客户1.名称1:width=35&height=35&unit=px&limit=9&row=3}
         *  客户1.名称1:width=35&height=35&unit=px&limit=9&row=3
         */
        String[] placeHolders = HaiweiPatternUtil.parsePlaceHolder(sign);
        if(HaiweiStringUtil.isBlank(placeHolders[1])){
            throw new HaiweiSignException("sign[ " + sign +  " ] is invalid");
        }
    
        /**
         * 客户1.名称1:width=35&height=35&unit=px&limit=9&row=3
         * path ： 客户1.名称1
         * param ： width=35&height=35&unit=px&limit=9&row=3
         */
        String[] pathAndParms = placeHolders[1].split(HaiweiSignConstant.PATH_AND_PARMS_SEPARATOR);
        
        final String path = getSginPart(pathAndParms, HaiweiSignConstant.SIGN_CONTEXT_COMPOSE.PATH);
        if(HaiweiStringUtil.isEmpty(path)){
            throw new HaiweiSignException("sign must contain path,sign: " + sign);
        }
        
        // 构建返回值
        HaiweiTextSign baseSign = new HaiweiTextSign();
        
        final String param = getSginPart(pathAndParms, HaiweiSignConstant.SIGN_CONTEXT_COMPOSE.PARAM);
        if(HaiweiStringUtil.isNotEmpty(param)){
            /**
             * width=35&height=35&unit=px&limit=9&row=3
             * width=35
             */
            final String[] params = param.split(HaiweiSignConstant.SGIN_PARMS_SEPARATOR);
            if(HaiweiArrayUtil.isEmpty(params)){
                for(String p1 : params){
                    if(HaiweiStringUtil.isNotEmpty(p1)){
                        // width=35
                        final String[] keyAndValue = p1.split(HaiweiSignConstant.SGIN_PARAM_KEY_VALUE_SEPARATOR);
                        if(HaiweiArrayUtil.lengthEqual(keyAndValue,2)
                            && !HaiweiStringUtil.hashEmpty(keyAndValue)){
                            baseSign.getParams().put(keyAndValue[0],keyAndValue[1]);
                        }
                    }
                }
            }
        }
    
        baseSign.setSignContext(sign);
        baseSign.setPath(path);
        baseSign.setBeforSignContext(placeHolders[0]);
        baseSign.setAfterSignContext(placeHolders[2]);
        return baseSign;
        
    }
    
    private static String getSginPart(String[] pathAndParms,int position){
        return HaiweiArrayUtil.length(pathAndParms) >= position
                ? pathAndParms[position - 1] : "";
    }


}
